﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ResourceRequest.Services;
using ePlanet.ResourceRequest.Models;
using Microsoft.Practices.Unity;
using ePlanet.ResourceRequest.DataAccess;

namespace Web.Views.TempUserManage
{
    public partial class DelayTempUser : System.Web.UI.Page
    {
        [Dependency]
        public ITempUserService m_ITempUserService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDelayUser_Click(object sender, EventArgs e)
        {
            try
            {
                string loginName = this.txtLoginName.Text.Trim();
                string password = this.txtPassword.Text.Trim();
                if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(password))
                {
                    throw new Exception("登录名或密码不能为空。");
                }

                TempUser.TEMPUSERRow user = this.m_ITempUserService.GetUser(loginName, password);
                if (user != null)
                {
                    if ((int)user.STATUSID != (int)TempUserStausEnum.CanLogin)
                    {
                        throw new Exception("您的账户不允许登录，请联系管理员。");
                    }

                    if (user.EXPIRATIONTIME > DateTime.Now)
                    {
                        throw new Exception("您的账户尚未过期，不需要延期。");
                    }

                    if (!string.IsNullOrEmpty(this.txtExpirationTime.Text.Trim()))
                    {
                        user.EXPIRATIONTIME = DateTime.Parse(this.txtExpirationTime.Text.Trim());
                    }

                    if (this.m_ITempUserService.DelayUser(user))
                    {
                        ClientScript.RegisterStartupScript(this.GetType()
                    , "btnDelayUser_Click"
                    , string.Format("<script>alert(\"您的账户延期成功\")</script>"));
                    }
                    else
                    {

                        ClientScript.RegisterStartupScript(this.GetType()
                    , "btnDelayUser_Click"
                    , string.Format("<script>alert(\"您的账户延期失败\")</script>"));
                    }

                }
                else
                {
                    throw new Exception("账号或密码错误");
                }
            }
            catch (Exception ex)
            {
                //Response.Write("<script>alert(\"" + ex.Message + "\")</script>");
                ClientScript.RegisterStartupScript(this.GetType()
                    , "btnDelayUser_Click"
                    , string.Format("<script>alert(\"" + ex.Message + "\")</script>"));
            }
        }
    }
}